<?php

class Frmcreator_Model_DbTable_Forms extends Engine_Db_Table {

    protected $_rowClass = 'Frmcreator_Model_Form';

    public function getFormsSelect($params = array()) {
        $table = Engine_Api::_()->getDbtable('forms', 'frmcreator');
        $rName = $table->info('name');
        $select = $table->select()->order($rName . '.creation_date');
        if (isset($params['status'])) {
            $select->where('status = ?', $params['status']);
        }
        return $select;
    }

    public function getFormsPaginator($params = array()) {
        $paginator = Zend_Paginator::factory($this->getFormsSelect($params));
        return $paginator;
    }

    public function createForm($formdata) {
        $form_settings = array();
        $fsettings = explode("&", urldecode($formdata));
        foreach ($fsettings as $row) {
            list($key, $val) = explode("=", $row);
            $form_settings[$key] = $val;
        }
        $form_settings['modified_date'] = date('Y-m-d H:i:s');
        $form_settings['creation_date'] = date('Y-m-d H:i:s');

        $this->insert($form_settings);
        $latestID = $this->getAdapter()->lastInsertId();
        if ($latestID && $form_settings['status']) {
            $menuitems = Engine_Api::_()->getDbtable('menuItems', 'core');
            $url = $form_settings['url'];
            $items = array(
                'name' => 'frmcreator_footer_' . $url,
                'module' => 'frmcreator',
                'label' => $form_settings['title'],
                'plugin' => '',
                'params' => '{"route":"frmcreator_view","action":"' . $url . '"}',
                'menu' => 'core_footer',
                'submenu' => '',
                'enabled' => 1,
                'custom' => 0,
                'order' => 999
            );
            $menuitems->insert($items);
        }

        return $latestID;
    }

    public function updateForm($formdata, $form_id) {

        $form_settings = array();
        $fsettings = explode("&", urldecode($formdata));
        foreach ($fsettings as $row) {
            list($key, $val) = explode("=", $row);
            $form_settings[$key] = $val;
        }
        $form_settings['modified_date'] = date('Y-m-d H:i:s');

        //Update
        $where = $this->getAdapter()->quoteInto('form_id = ?', $form_id);
        $this->update($form_settings, $where);
        ;

        //menu items processing
        $menuitems = Engine_Api::_()->getDbtable('menuItems', 'core');
        $url = $form_settings['url'];
        $items = array(
            'name' => 'frmcreator_footer_' . $url,
            'module' => 'frmcreator',
            'label' => $form_settings['title'],
            'plugin' => '',
            'params' => '{"route":"frmcreator_view","action":"' . $url . '"}',
            'menu' => 'core_footer',
            'submenu' => '',
            'enabled' => 1,
            'custom' => 0,
            'order' => 999
        );

        if ($form_id && $form_settings['status']) {
            //$menuitems->insert($items);
            $menu = Engine_Api::_()->frmcreator()->getFormFooterMenu($items, true);
            
        } else if ($form_id && !$form_settings['status']) {
            $select = $menuitems->select(Zend_Db_Table::SELECT_WITH_FROM_PART)
                    ->setIntegrityCheck(false)
                    ->where("name = 'frmcreator_footer_$url'");
            $result = $menuitems->fetchRow($select);        
            
            if($result){
                //Remove Menu out Footer
                $result->delete();
            }
        }
    }

    public function removeMenuItem($form_id) {
        //get form
        $form = Engine_Api::_()->getItem('frmcreator_form', $form_id);
        
        if($form){
            //$form = $form->toArray();
            // select menu item
            $menuitems = Engine_Api::_()->getDbtable('menuItems', 'core');
            $name = 'frmcreator_footer_' . $form->url;
            $select = $menuitems->select()             
                        ->where('name = ?', $name);
            
            $result = $menuitems->fetchRow($select);
            if($result){
                $result->delete();
            }
        }
    }
    
    public function getFormByUrl($url){
        if(empty($url)){
            return NULL;
        }        
        $select = $this->select();        
        $select->where("url = ?", $url);
        //This form must be ENABLE
        $select->where('status = 1');        
        
        $form = $this->fetchRow($select);        
        return $form; 
    }

}
